package com.RedRiver.hospitalsystem.record_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.finance_service.client.BillClient;
import com.RedRiver.hospitalsystem.finance_service.client.param.BillDetailFromClient;
import com.RedRiver.hospitalsystem.finance_service.client.param.CreateBillFromClient;
import com.RedRiver.hospitalsystem.medicine_service.client.MedicineClient;
import com.RedRiver.hospitalsystem.medicine_service.client.param.FindMedicineByIdInfoClient;
import com.RedRiver.hospitalsystem.medicine_service.client.param.FindMedicineByIdInfoListClient;
import com.RedRiver.hospitalsystem.medicine_service.client.param.FindMedicineByIdListFromClient;
import com.RedRiver.hospitalsystem.record_service.common.Record_serviceResultCode;
import com.RedRiver.hospitalsystem.record_service.common.exception.PrescriptionMedicineException;
import com.RedRiver.hospitalsystem.record_service.form.CreatePrescriptionForm;
import com.RedRiver.hospitalsystem.record_service.form.MedicineForm;
import com.RedRiver.hospitalsystem.record_service.info.GetPersciptionInfo;
import com.RedRiver.hospitalsystem.record_service.info.MedicineInfo;
import com.RedRiver.hospitalsystem.record_service.infra.mapper.PrescriptionMapper;
import com.RedRiver.hospitalsystem.record_service.infra.mapper.PrescriptionMedicineMapper;
import com.RedRiver.hospitalsystem.record_service.infra.model.Prescription;
import com.RedRiver.hospitalsystem.record_service.infra.model.PrescriptionMedicine;
import com.RedRiver.hospitalsystem.record_service.service.PrescriptionMedicineService;
import com.RedRiver.hospitalsystem.record_service.service.PrescriptionService;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements PrescriptionService {

    @Resource
    private MedicineClient medicineClient;

    @Resource
    private BillClient billClient;

    @Resource
    private PrescriptionMedicineMapper prescriptionMedicineMapper;

    @Override
    public Result<?> addPerscription(CreatePrescriptionForm prescriptionForm) {


        FindMedicineByIdListFromClient medicine = new FindMedicineByIdListFromClient();
        List<Integer> collect = prescriptionForm.getList().stream().map(medic -> medic.getMedicineId()).collect(Collectors.toList());
        medicine.setList(collect);
        Result<FindMedicineByIdInfoListClient> result = medicineClient.findMedicineByIdList(medicine);
        if (result.getCode()==200&&result.getData().getList().size()==prescriptionForm.getList().size()) {

            BigDecimal decimal = BigDecimal.valueOf(0.00);
            List<BillDetailFromClient> billDetailFromClients = new ArrayList();
            for (FindMedicineByIdInfoClient infoClient : result.getData().getList()) {
                for (MedicineForm form : prescriptionForm.getList()) {
                    BillDetailFromClient client = new BillDetailFromClient();
                    if (infoClient.getMedicineId()==form.getMedicineId()) {
                        client.setTypeId(infoClient.getMedicineId());
                        client.setBillDetailPrice(infoClient.getMedicineRetailprice());
                        client.setBillDetailNumber(form.getMedicineNumber());
                        billDetailFromClients.add(client);
                        BigDecimal multiply = infoClient.getMedicineRetailprice().multiply(BigDecimal.valueOf(Double.parseDouble(form.getMedicineNumber() + ".00")));
                        decimal.add(multiply);
                    }
                }
            }

            //创建处方
            Prescription prescription = BeanUtil.copyProperties(prescriptionForm, Prescription.class);
            prescription.setMedicineTotal(decimal);
            int insert = baseMapper.insert(prescription);
            if (insert>0) {
                int i=0;
                List<PrescriptionMedicine> list = BeanUtil.copyToList(prescriptionForm.getList(), PrescriptionMedicine.class);
                for (PrescriptionMedicine prescriptionMedicine : list) {
                    prescriptionMedicine.setPrescriptionId(prescription.getPrescriptionId());
                    i+=prescriptionMedicineMapper.insert(prescriptionMedicine);
                }
                if (i==prescriptionForm.getList().size()) {
                    CreateBillFromClient bill = new CreateBillFromClient();
                    bill.setPaymentId(1);
                    bill.setPatientId(prescriptionForm.getPatientId());
                    bill.setWorkerId(prescriptionForm.getDoctorId());
                    bill.setBillPrice(decimal);
                    bill.setList(billDetailFromClients);
                    bill.setCheckId(prescription.getPrescriptionId());
                    billClient.createBill(bill);
                    return  Result.getInstance().setMsg("创建处方成功");
                }
            }throw new PrescriptionMedicineException(Record_serviceResultCode.CREATE_PRESCRIPTION_ERROR);
        }//药物不存在
        throw new PrescriptionMedicineException(Record_serviceResultCode.MEDICINES_NOT_EXISTS_ERROR);
    }

    @Override
    public GetPersciptionInfo searchPersciption(Integer id) {
        Prescription prescription = baseMapper.selectById(id);
        if (prescription!=null) {
            GetPersciptionInfo persciptionInfo = BeanUtil.copyProperties(prescription, GetPersciptionInfo.class);
            List<PrescriptionMedicine> medicines = prescriptionMedicineMapper.selectList(new QueryWrapper<PrescriptionMedicine>().eq("prescription_id", id));
            if (medicines.size()!=0) {
                List<MedicineInfo> medicineInfos = BeanUtil.copyToList(medicines, MedicineInfo.class);
                persciptionInfo.setList(medicineInfos);
            }
            return persciptionInfo;
        }
        throw new PrescriptionMedicineException(Record_serviceResultCode.PERSCRIPTION_NOT_EXSTS_ERROR);
    }
}
